<?php
namespace app\entries\controller;
use think\Controller;
// use think\Db;

/**
 *
 */
class Area extends Logincheck
{
    //返回产地名称
    function area()
    {
        $teacase = model("Area")->teacase();
        $this->assign('teacase', $teacase );
        $teaarea = teaarea_ids();

        $caseArea=[];
        foreach ($teacase as $case) {
            $onedata=[];
            $areas=[];
            $onedata['tid'] = $case["id"];
            $onedata["teacase"] = $case["teaname"];
            $retArea = model("Area")->caseArea($case["id"]);
            if(!$retArea){
                $onedata["areas"] = "暂无产区";
            }else{
                foreach ($retArea as $aid) {
                    $areas['aid'] = $aid['teaarea_id'];
                    $areas['area'] = $teaarea[$aid['teaarea_id']];
                    $areas['display'] = $aid['display'];
                    $areas['source'] = $aid['source'];
                    $onedata['areas'][] = $areas;
                }
            }
            $caseArea[] = $onedata;
        }
        // print_pre($caseArea);
        $this->assign('caseAreas', $caseArea );
        return $this->fetch();
    }

    //增加产区并关联茶类
    function insertArea($tid, $area,$source)
    {
        $aid = model("Area")->findArea($area);
        if(count($aid)==2){
            //判断茶类和产区是否建立了关联
            $area_case =model("Area")->findAreaCase($tid, $aid['id']);
            if($area_case){
                return $this->error('产区已存在');
            }else{
                model("Area")->create_relate($tid, $aid['id'], $source);
                return $this->success('添加成功');
            }
        }else{
            //产区不存在
            model("Area")->insertArea($tid,$area, $source);
            return $this->success('添加成功');
        }
    }

    //删除产区
    function delArea($dtid, $daid)
    {
        $retval = model('Area')->delArea($dtid, $daid);
        return $this->success('删除成功');
    }

    //是否展示
    function displayarea($tid, $aid, $display)
    {
        if($display==1){
            $data = ['display'=>0];
        }else{
             $data = ['display'=>1];
        }
        $condition = ['teacase_id'=>$tid, 'teaarea_id'=>$aid];
        $retval = db('case_area')->where($condition)->update($data);
        return $retval;
    }

    //修改来源
    function mdsource($tid, $aid, $source)
    {
        $condition = ['teacase_id'=>$tid, 'teaarea_id'=>$aid];
        $data = ['source'=>$source];
        db('case_area')->where($condition)->update($data);
        return $this->success("修改成功");
    }














}

?>